package com.ljm.sys.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Splitter;
import com.ljm.common.dto.UserInfoDto;
import com.ljm.security.api.SecurityApi;
import com.ljm.security.dto.SecurityResRule;
import com.ljm.security.dto.SecurityUserDetail;
import com.ljm.security.util.UserUtil;
import com.ljm.sys.auth.dto.AuthoritiesDto;
import com.ljm.sys.auth.entity.SysAuthResource;
import com.ljm.sys.auth.entity.SysAuthUserRole;
import com.ljm.sys.auth.service.ISysAuthResourceService;
import com.ljm.sys.auth.service.ISysAuthRoleResourceService;
import com.ljm.sys.auth.service.ISysAuthUserRoleService;
import com.ljm.sys.org.entity.SysOrgUser;
import com.ljm.sys.org.service.ISysOrgUserService;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: ChenHuaMing
 * @Date: 2020/6/11 13:26
 * @Description:
 */
@Service
public class SecurityImpl implements SecurityApi {
    private final static MapperFacade MAPPER_FACADE;
    static {
        MapperFactory mapperFactory=new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(SysOrgUser.class,UserInfoDto.class).byDefault().register();
        MAPPER_FACADE=mapperFactory.getMapperFacade();
    }
    @Resource
    private ISysOrgUserService sysOrgUserService;
    @Resource
    private ISysAuthRoleResourceService sysAuthRoleResourceService;
    @Resource
    private ISysAuthUserRoleService sysAuthUserRoleService;
    @Resource
    private ISysAuthResourceService sysAuthResourceService;

    @Override
    public SecurityUserDetail getUser(String userName) {
        SysOrgUser user = sysOrgUserService.getOne(new LambdaQueryWrapper<SysOrgUser>().eq(SysOrgUser::getLoginName, userName));
        if(user==null){
            return null;
        }else{
            SecurityUserDetail userDetail=new SecurityUserDetail(user.getLoginName(),user.getLoginPwd(),true,true,true,false);
            Set<AuthoritiesDto> authorites = sysAuthRoleResourceService.getAuthorites(String.valueOf(user.getId()));
            if(CollectionUtils.isNotEmpty(authorites)){
                authorites.forEach(auth->{
                    userDetail.getAuthorities().add(auth.getRoleCode());
                    userDetail.getAuthorities().add(auth.getResFullPath());
                });
            }
            return userDetail;
        }
    }

    @Override
    public UserInfoDto getUserInfo(String userName) {
        SysOrgUser user = sysOrgUserService.getOne(new LambdaQueryWrapper<SysOrgUser>().eq(SysOrgUser::getLoginName, userName));
        if(null==user){
            return null;
        }else{
            UserInfoDto userInfoDto=new UserInfoDto();
            MAPPER_FACADE.map(user,userInfoDto);
            return userInfoDto;
        }
    }

    @Override
    public SecurityResRule getResRule(String url) {
        SysAuthResource res = sysAuthResourceService.getOne(new LambdaQueryWrapper<SysAuthResource>().eq(SysAuthResource::getResFullPath, url));
        if(res!=null){
            SecurityResRule resRule=new SecurityResRule();
            resRule.setUrl(res.getResFullPath());
            resRule.setFdLoginVisible(res.getFdLoginVisible());
            return resRule;
        }else{
            return null;
        }
    }

    @Override
    public Set<String> getRole(String userName) {
        List<SysAuthUserRole> roleCodes = sysAuthUserRoleService.list(new LambdaQueryWrapper<SysAuthUserRole>().eq(SysAuthUserRole::getUserId, UserUtil.getCurUser().getId()));
        if(CollectionUtils.isNotEmpty(roleCodes)){
            return roleCodes.stream().map(SysAuthUserRole::getRoleCode).collect(Collectors.toSet());
        }else{
            return Collections.emptySet();
        }
    }

    @Override
    public Set<String> getPermission(String userName) {
        Set<String> permission = sysAuthRoleResourceService.getPermission(UserUtil.getCurUser().getId());
        if(CollectionUtils.isNotEmpty(permission)){
            return permission;
        }else{
            return Collections.emptySet();
        }
    }

    @Override
    public Set<String> getPermissionForFront(String userName) {
        Set<String> permission = sysAuthRoleResourceService.getPermissionForFront(UserUtil.getCurUser().getId());
        if(CollectionUtils.isNotEmpty(permission)){
            Set<String> permissionSet=new HashSet<>();
            permission.forEach(permissionTemp->{
                Splitter.on("->").split(permissionTemp).forEach(permissionSet::add);
            });
            return permissionSet;
        }else{
            return Collections.emptySet();
        }
    }
}
